package com.neusoft.neuiotms.dcom.converter;

import java.text.SimpleDateFormat;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.common.utils.DateUtil;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.TransferWeeklyDTO;
import com.neusoft.neuiotms.dcom.model.TransferWeekly;
import com.neusoft.neuiotms.dcom.repository.TransferWeeklyRepository;
import com.neusoft.neuiotms.dcom.vo.TransferWeeklyExportVO;
import com.neusoft.neuiotms.dcom.vo.TransferWeeklyImportVO;

@Component
public class TransferWeeklyConverter extends AbstractConverter<TransferWeekly, TransferWeeklyDTO> {

    @Autowired
    private TransferWeeklyRepository transferWeeklyRepository;
    @Autowired
    private BaseIdGenerator transferWeeklyCodeGenerator;

    @Override
    public TransferWeekly toModel(final TransferWeeklyDTO dto) {
        TransferWeekly model;
        if (dto.isNew()) {
            model = new TransferWeekly();
            if (StringUtils.isNotBlank(dto.getCode())) {
                model.setCode(dto.getCode());
            } else {
                model.setCode(this.transferWeeklyCodeGenerator.generateCode());
            }
        } else {
            model = this.transferWeeklyRepository.getOne(dto.getId());
        }

        model.setUnitCode(dto.getUnitCode());
        model.setUnitName(dto.getUnitName());
        model.setUserCode(dto.getUserCode());
        model.setSurvey(dto.getSurvey());
        model.setHealthCheckup(dto.getHealthCheckup());
        model.setDropIn(dto.getDropIn());
        model.setSchedule(dto.getSchedule());
        model.setMaterial(dto.getMaterial());
        model.setClosingTime(dto.getClosingTime());
        model.setAssetInventory(dto.getAssetInventory());
        model.setAssetWarehousing(dto.getAssetWarehousing());
        model.setAssetEntry(dto.getAssetEntry());
        model.setSchemeCustomization(dto.getSchemeCustomization());
        model.setSummaryDescription(dto.getSummaryDescription());
        model.setWorkPlan(dto.getWorkPlan());
        model.setProblemLog(dto.getProblemLog());
        model.setRemarks(dto.getRemarks());
        model.setWeekTime(DateUtil.getStartTimeOfWeek(dto.getWeekTime()));
        model.setWeekDescription(dto.getWeekDescription());

        return model;
    }

    @Override
    public TransferWeeklyDTO toDTO(final TransferWeekly model, final boolean forListView) {

        final TransferWeeklyDTO dto = new TransferWeeklyDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setUnitCode(model.getUnitCode());
        dto.setUnitName(model.getUnitName());
        dto.setUserCode(model.getUserCode());
        dto.setSurvey(model.getSurvey());
        dto.setHealthCheckup(model.getHealthCheckup());
        dto.setDropIn(model.getDropIn());
        dto.setSchedule(model.getSchedule());
        dto.setMaterial(model.getMaterial());
        dto.setClosingTime(model.getClosingTime());
        dto.setAssetInventory(model.getAssetInventory());
        dto.setAssetWarehousing(model.getAssetWarehousing());
        dto.setAssetEntry(model.getAssetEntry());
        dto.setSchemeCustomization(model.getSchemeCustomization());
        dto.setSummaryDescription(model.getSummaryDescription());
        dto.setWorkPlan(model.getWorkPlan());
        dto.setProblemLog(model.getProblemLog());
        dto.setRemarks(model.getRemarks());
        dto.setWeekTime(DateUtil.getStartTimeOfWeek(model.getWeekTime()));
        dto.setWeekDescription(model.getWeekDescription());

        return dto;
    }

    public TransferWeeklyExportVO toVo(final TransferWeeklyDTO dto) {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
        final TransferWeeklyExportVO vo = new TransferWeeklyExportVO();
        vo.setUnitName(dto.getUnitName());
        vo.setSurvey(dto.getSurvey() + DCOMConstants.PERCENT);
        vo.setDropIn(dto.getDropIn() + DCOMConstants.PERCENT);
        vo.setAssetInventory(dto.getAssetInventory() + DCOMConstants.PERCENT);
        vo.setMaterial(dto.getMaterial() + DCOMConstants.PERCENT);
        vo.setAssetEntry(dto.getAssetEntry() + DCOMConstants.PERCENT);
        vo.setAssetWarehousing(dto.getAssetWarehousing() + DCOMConstants.PERCENT);
        vo.setSchemeCustomization(dto.getSchemeCustomization() + DCOMConstants.PERCENT);
        vo.setHealthCheckup(dto.getHealthCheckup() + DCOMConstants.PERCENT);
        vo.setSchedule(dto.getSchedule() + DCOMConstants.PERCENT);
        vo.setClosingTime(dto.getClosingTime() == null ? "" : format.format(dto.getClosingTime()));
        vo.setWorkPlan(dto.getWorkPlan());
        vo.setProblemLog(dto.getProblemLog());
        vo.setRemarks(dto.getRemarks());
        vo.setSummaryDesc(dto.getSummaryDescription());
        vo.setThisWeek(dto.getWeekDescription());
        return vo;
    }

    public TransferWeekly toModel(final TransferWeeklyImportVO vo) {
        final TransferWeekly model = new TransferWeekly();
        model.setAssetEntry(vo.getAssetEntry());
        model.setAssetInventory(vo.getAssetInventory());
        model.setAssetWarehousing(vo.getAssetWarehousing());
        model.setClosingTime(vo.getClosingTime());
        model.setCode(this.transferWeeklyCodeGenerator.generateCode());
        model.setDropIn(vo.getDropIn());
        model.setHealthCheckup(vo.getHealthCheckup());
        model.setMaterial(vo.getMaterial());
        model.setProblemLog(vo.getProblemLog());
        model.setRemarks(vo.getRemarks());
        model.setSchedule(vo.getSchedule());
        model.setSchemeCustomization(vo.getSchemeCustomization());
        model.setSummaryDescription(vo.getSummaryDescription());
        model.setSurvey(vo.getSurvey());
        model.setUnitCode(vo.getUnit().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        model.setUnitName(vo.getUnit().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        model.setWeekTime(DateUtil.getStartTimeOfWeek(vo.getWeekTime()));
        model.setWorkPlan(vo.getWorkPlan());
        return model;
    }
}
